###################################################
################## Section 3, sub-section 3.1 
################## Firm-bank relationships 
################## Plot results 
###################################################

# import libraries
import os
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

path_data = r'P:\ECB business areas\DGR\Staff\papouts\Research\Active\Firm Heterogeneities\Final datasets'
path_charts = r'P:\ECB business areas\DGR\Staff\papouts\Research\Active\Firm Heterogeneities\Final charts\Relationships\order by large firms\final'
if not os.path.isdir(path_charts):
    os.mkdir(path_charts)

    
# define plotting function
def plot_nmbr_crdtr(data, color, order):
    # data should be a dataframe
    # color should be a text
    # order should be a list of lists, where the first is order of all countries and the second is selected countries    
    # single plots for selected countries
    
    sns.set(rc={'figure.figsize':(15,10), 'axes.labelsize':26, 'axes.titlesize':26,  'axes.labelpad':15, 'xtick.labelsize':26, 'ytick.labelsize':26}, style='white')
    for i, j in enumerate(['Large', 'Medium', 'Small', 'Micro']):
        plt.figure()
        sns.boxplot(data=data.loc[data['CNTRY'].isin(order[1])][data.loc[data['CNTRY'].isin(order[1])].sz_f == (i+1)], x='CNTRY', y='NMBR_CRDTRS', width=[0.6], whis=[25, 75], showfliers=False, showmeans=True, medianprops={'color':'red', 'linewidth':4}, meanprops={'markerfacecolor':'red', 'markeredgecolor':'red', 'markersize':12}, color=color, order=order[1]).set(xlabel='', ylabel='Number of firm-bank relationships')
        plt.savefig(path_charts + '\\Section3_nrel_firm_bank_bysz_' + j + '_201912.pdf', dpi=600, bbox_inches='tight')

    
# load data
AC_final = pd.read_stata(path_data + r'\\dbtr_lvl_dt_f.dta')
AC_final.rename(columns={'cntry_dbtr':'CNTRY','nmbr_crdtrs':'NMBR_CRDTRS'}, inplace=True)
AC_final.drop(AC_final[AC_final.nace_code == ''].index, inplace=True)
AC_final.loc[AC_final['NMBR_CRDTRS'] > 13, 'NMBR_CRDTRS'] = 13
print(AC_final.NMBR_CRDTRS.describe())

# plot firm-bank relationships 
# order
order_cntry_alph = ['AT','BE','CY','DE','EE','ES','FI','FR','GR','IE','IT','LT','LU','LV','MT','NL','PT','SI','SK']
sel_countries_alph = ['AT','BE','DE','ES','FI','FR','GR','IE','IT','NL','PT']
order_cntry = AC_final[AC_final.sz_f == 1].groupby(['CNTRY'])['NMBR_CRDTRS'].mean().sort_values(ascending=False).index
order_slctd_cntry = order_cntry[order_cntry.isin(sel_countries_alph)]

# plot EA11
plot_nmbr_crdtr(data=AC_final, color='#7ccaa5', order=[order_cntry, order_slctd_cntry])
               
               
